package com.kb.design.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kb.design.common.ApiResponse;
import com.kb.design.common.ResponseObject;
import com.kb.design.dto.DesignOrderDTO;
import com.kb.design.dto.DesignOrderDetailDTO;
import com.kb.design.entity.CustomUser;
import com.kb.design.entity.DesignOrder;
import com.kb.design.service.ContractService;
import com.kb.design.service.DesignOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;;
import java.util.List;

@RestController
@RequestMapping("/designOrder")
public class DesignOrderController {

    @Autowired
    private DesignOrderService designOrderService;
    @Resource
    private ContractService contractService;

    @PostMapping("/list")
    public ApiResponse list(@RequestBody DesignOrderDTO designOrder) {
        ResponseObject responseObject = new ResponseObject();
        Page<DesignOrderDTO> customUserPage = designOrderService.queryByPage(designOrder);
        responseObject.setItems(customUserPage.getRecords());
        responseObject.setTotal(customUserPage.getTotal());
        return ApiResponse.success(responseObject);
    }
    @PostMapping("/listByParameters")
    public ApiResponse listByParameters(@RequestBody DesignOrderDTO designOrder) {
        ResponseObject responseObject = new ResponseObject();
        Page<DesignOrder> orderPage = designOrderService.listByParameters(designOrder);
        responseObject.setItems(orderPage.getRecords());
        responseObject.setTotal(orderPage.getTotal());
        return ApiResponse.success(responseObject);
    }
    @PostMapping("/getDetailInfo")
    public ApiResponse getOrderDetailInfo(@RequestBody DesignOrderDTO designOrder) {
        DesignOrderDetailDTO detailInfo = designOrderService.getDetailInfo(designOrder);
        return ApiResponse.success(detailInfo);
    }

    @PostMapping
    public boolean save(@RequestBody DesignOrder designOrder) {
        return designOrderService.saveDesignOrder(designOrder); // 修改为调用 saveDesignOrder 方法
    }

    @PutMapping
    public boolean update(@RequestBody DesignOrder designOrder) {
        return designOrderService.updateDesignOrder(designOrder); // 修改为调用 updateDesignOrder 方法
    }

    @DeleteMapping("/{id}")
    public boolean delete(@PathVariable Long id) {
        return designOrderService.deleteDesignOrder(id); // 修改为调用 deleteDesignOrder 方法
    }

    @GetMapping("/{id}")
    public DesignOrder getById(@PathVariable Long id) {
        return designOrderService.getDesignOrderById(id); // 修改为调用 getDesignOrderById 方法
    }

    // 新增根据合同号查询设计订单的方法
    @GetMapping("/getByContractNumber")
    public ApiResponse getByContractNumber(String contractNumber) {
       return ApiResponse.success(contractService.selectByContractNumber(contractNumber));
    }
    // 拉取ERP最新订单数据
    @GetMapping("/ERP2DesignCooperate")
    public ApiResponse ERP2DesignCooperate(Integer date) {
        return ApiResponse.successAndMsg(designOrderService.designOrderService(date),"拉取成功");
    }

    /**
     *  ERP 取消订单推送
     * @param
     * @return
     */
    @GetMapping("/cancelOrder")
    public ApiResponse cancelOrder(@RequestBody DesignOrderDTO designOrder) {
        return ApiResponse.success(designOrderService.cancelOrder(designOrder));
    }
}